package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinesssData() {

        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(today, LocalTime.now());
        //营业额
        //有效订单数
        //平均客单价
        BusinessDataVO businessDataVO = orderMapper.getBusinessData(begin,end);
        if(businessDataVO == null){
            businessDataVO = new BusinessDataVO();
            businessDataVO.setTurnover(0.0);
            businessDataVO.setValidOrderCount(0);
            businessDataVO.setUnitPrice(0.0);
        }

        //订单完成率
        Integer total = orderMapper.getTodayOrdersCount(begin,end);
        if(total != 0) {

            Integer validOrder = businessDataVO.getValidOrderCount();
            Double orderCompletionRate = null;
            if (validOrder == null) {
                orderCompletionRate = 0.0;
            } else orderCompletionRate = validOrder / (double) total;
            businessDataVO.setOrderCompletionRate(orderCompletionRate);
        }else businessDataVO.setOrderCompletionRate(0.0);
        // 新增用户数
        Map map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        Integer newUsers = userMapper.getUserCount(map);
        businessDataVO.setNewUsers(newUsers);

        return businessDataVO;
    }

    @Override
    public SetmealOverViewVO setmealOverView() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(setmealMapper.getDishCount(1));
        setmealOverViewVO.setDiscontinued(setmealMapper.getDishCount(0));
        return setmealOverViewVO;
    }

    @Override
    public DishOverViewVO dishOverView() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(dishMapper.getDishCount(1));
        dishOverViewVO.setDiscontinued(dishMapper.getDishCount(0));
        return dishOverViewVO;
    }

    @Override
    public OrderOverViewVO getOrderOver() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(orderMapper.getOrders(null));
        orderOverViewVO.setCompletedOrders(orderMapper.getOrders(Orders.COMPLETED));
        orderOverViewVO.setCancelledOrders(orderMapper.getOrders(Orders.CANCELLED));
        orderOverViewVO.setWaitingOrders(orderMapper.getOrders(Orders.TO_BE_CONFIRMED));
        orderOverViewVO.setDeliveredOrders(orderMapper.getOrders(Orders.DELIVERY_IN_PROGRESS));
    return orderOverViewVO;
    }
}
